package it.lovaex.scb.Cart;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public class Receipt {
	
	BigDecimal rawTotal; 
	BigDecimal finalTotal; 
	BigDecimal discountRate;
	List<CartItem> itemsList;
	
	public Receipt(List<CartItem> itemsList, BigDecimal rawTotal, BigDecimal finalTotal, BigDecimal discountRate){
		this.itemsList = itemsList;
		this.rawTotal = rawTotal;
		this.finalTotal = finalTotal;
		this.discountRate = discountRate;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("\t\t| Quantity | UnitPrice | Discount | Final Price |\n");
		for (CartItem cartItem : itemsList) {
			builder.append(cartItem.toString());
		}
		if (discountRate.compareTo(BigDecimal.ZERO) > 0){
			builder.append("GrossTotal\t\t\t\t\t\t\t" + rawTotal.setScale(2, RoundingMode.CEILING) + "\n");
			builder.append("VolumeDiscount\t\t\t\t" + 
					discountRate.multiply(BigDecimal.valueOf(100)).setScale(0, RoundingMode.CEILING) + "%\t" + 
					rawTotal.subtract(finalTotal).setScale(2, RoundingMode.CEILING) + "\n");
		}
		
		builder.append("Total\t\t\t\t\t\t\t\t" + finalTotal.setScale(2, RoundingMode.CEILING));
		return builder.toString();
	}
}
